package com.example.huitong.ui;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.MenuItem;
import android.widget.TextView;

import com.example.huitong.App;
import com.example.huitong.R;
import com.example.huitong.bean.LogisticInfo;
import com.example.huitong.util.Constant;
import com.example.huitong.util.NetHelper;

public class LogisticDetailsActivity extends FragmentActivity {

	private ProgressDialog mDialog;
	private TextView mTextView;
	private TextView mTvTitle;
	private TextView mTvState;
	
	private static final int MSG_OK = 0;
	private static final int MSG_ERR = 1;
	private static final int MSG_UNKNOW = 2;
	
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_OK:
				LogisticInfo info = (LogisticInfo) msg.obj;
				ArrayList<LogisticInfo.Data> list = info.dataList;
				StringBuilder sb = new StringBuilder();
				for(LogisticInfo.Data data : list){
					sb.append(
							data.time).append("\n    ").append(data.context)
							.append("\n");
				}
				mTextView.setText(sb.toString());
				mTvState.setText("当前状态: " + info.parseState());
				break;
			case MSG_ERR:
				LogisticInfo info2 = (LogisticInfo) msg.obj;
				mTextView.setText("查询失败，errCode=" + info2.message);
				break;
				
			case MSG_UNKNOW:
				mTextView.setText("未知错误");
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_logistic_details);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));
		initViews();
		
		handleData();
	}

	private void initViews() {
		// TODO Auto-generated method stub
		mTextView = (TextView) findViewById(R.id.textView1);
		mTvTitle = (TextView) findViewById(R.id.title);
		mTvState = (TextView) findViewById(R.id.state);
		getActionBar().setTitle("物流详情");
	}

	private void handleData() {
		// TODO Auto-generated method stub
		mDialog = ProgressDialog.show(this, null, null);
		
		String exp_id = getIntent().getStringExtra(Constant.KEY_EXP_ID);
		String num = getIntent().getStringExtra(Constant.KEY_EXP_NUM);
		String exp_name = getIntent().getStringExtra(Constant.KEY_EXP_NAME);
		
		mTvTitle.setText("[" + exp_name + ": " + num +"]");
		new QueryTask().execute(exp_id, num);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	class QueryTask extends AsyncTask<String, Void, LogisticInfo>{
		
		@Override
		protected LogisticInfo doInBackground(String... params) {
			// TODO Auto-generated method stub
			if(params.length != 2){
				return null;
			}
			String expId = params[0];
			String num = params[1];
			String content = NetHelper.getInstance().getExpressLogistics(expId, num);
			LogisticInfo info = LogisticInfo.constructInfo(content == null ? "" : content);
			return info;
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			if(!App.getApp().isNetworkConnected()){
				App.toastShort(LogisticDetailsActivity.this, "网络未连接");
			}
		}
		
		@Override
		protected void onPostExecute(LogisticInfo result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			dismissDialog(mDialog);
			System.out.println("AAA");
			if(result != null){
				System.out.println("onPostExecute: " + result);
				int status = result.status;
				if(status == 200){
					mHandler.obtainMessage(MSG_OK, result).sendToTarget();
				} else {
					mHandler.obtainMessage(MSG_ERR, result).sendToTarget();
				}
			} else {
				
			}
		}
		
	}
	private void dismissDialog(Dialog dialog){
		if(dialog != null && dialog.isShowing()){
			dialog.dismiss();
			dialog = null;
		}
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dismissDialog(mDialog);
	}
}
